package com.hsmpay.common.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsmpay.common.util.system.Constant;
import net.sf.json.JSONObject;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 三毛接口与现有银行表【Info_bank】对接工具类
 * @author Administrator
 */
public class ThreeMaoBankUtil {
    private static Map<Long, String> oTMap = new HashMap<Long, String>();
    public static String threeMaoBankIds;
    private static Map<String, String> bankMap = new HashMap<String, String>();

    static {
        assemble();
    }

    public static void assemble(){
        //从JSon文件读取数据
        StringBuffer stringBuffer = new StringBuffer();
        String line = null ;
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File(Constant.getRootPath()+File.separator+"WEB-INF/classes/"+"bankList.json")));
            while( (line = br.readLine())!= null ){
                stringBuffer.append(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String jsonAll = stringBuffer.toString().replace(" ","");
        JSONObject jsonObject = JSONObject.fromObject(jsonAll);
        String clientType = jsonObject.get("clientType").toString();
        String json = jsonObject.get(clientType).toString();
        List<Map<String,String>> list = null;
        try {
            list = parseToBeanList(json);
            StringBuilder sbr = new StringBuilder();
            for(int i=0;i<list.size();i++){
                Map<String,String> map = list.get(i);
                long id = Long.valueOf(map.get("id"));
                String code = map.get("code");
                String name = map.get("name");
                oTMap.put(id,code);
                bankMap.put(name,code);
                sbr.append(id+",");
            }
            threeMaoBankIds = sbr.substring(0,sbr.lastIndexOf(","));
            System.out.println(threeMaoBankIds);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将json转换为List<Map<String,String>>
     * @param json
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static List<Map<String,String>> parseToBeanList(String json) throws JsonParseException, JsonMappingException, IOException{
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(json, new TypeReference<List<HashMap<String, String>>>() {
        });
    }

    public static Map<Long, String> getOTMap() {
        return oTMap;
    }

    public static String getCode(Long bankId) {
        return oTMap.get(bankId);
    }

    public static String getSanMaoBankCode(String bankName) {
        return bankMap.get(bankName);
    }


}
